/*
 * @Author: leador-wcy 895308607@qq.com
 * @Date: 2024-10-22 14:56:59
 * @LastEditors: leador-wcy 895308607@qq.com
 * @LastEditTime: 2024-10-22 15:01:20
 * @FilePath: \lincao-web2\src\hooks\useModal.tsx
 * @Description: 避免因为使用useDrawer导致的定义混乱
 */
import { useState } from 'react';
import { useMemoizedFn } from 'ahooks';
import { Modal, ModalProps } from 'antd';

// 定义 useModal Hook
export default function useModal<T>(Comp: React.FunctionComponent<T>, modalProps?: ModalProps) {
    const [open, setIsOpen] = useState(false);
    const [props, setProps] = useState<any>();

    // 打开 Modal 的方法
    const openModal = useMemoizedFn((_props: Omit<T, 'onClose'>) => {
        setProps(_props);
        setIsOpen(true);
    });

    // 关闭 Modal 的方法
    const closeModal = useMemoizedFn(() => {
        setIsOpen(false);
    });

    return {
        open,
        openModal,
        closeModal,
        Content: (
            <Modal {...modalProps} open={open} onCancel={closeModal} footer={null} destroyOnClose>
                <Comp {...(props as any)} onClose={closeModal} />
            </Modal>
        ),
    };
}
